/*
 * TXMLSerializer.cpp
 *
 *  Created on: 2012-10-30
 *      Author: tutu
 */

#include "TXMLSerializer.h"
#include "../utils/TConverter.h"

TUserPointer TXMLSerializer::unserializeUser(QDomElement element)
{
	TUser *user = new TUser();
	user->setId(element.firstChildElement("id").text().toInt());
	user->setLoginName(element.firstChildElement("loginName").text());
	user->setLoginPassword(element.firstChildElement("loginPassword").text());
	user->setName(element.firstChildElement("name").text());
	user->setEmail(element.firstChildElement("email").text());
	TRolePointer role = unserializeRole(element.firstChildElement("role"));
	user->setRole(role);
	user->setActive(TConverter::stringToBool(element.firstChildElement("active").text()));
	user->setUpdateTime(TConverter::stringToDatetime(element.firstChildElement("updateTime").text()));
	user->setCreateTime(TConverter::stringToDatetime(element.firstChildElement("createTime").text()));
	if(!element.firstChildElement("latestLoginTime").isNull())
	{
		user->setLatestLoginTime(TConverter::stringToDatetime(element.firstChildElement("latestLoginTime").text()));
	}
	TUserPointer pointer(user);
	return pointer;
}

TRolePointer TXMLSerializer::unserializeRole(QDomElement element)
{
	TRole *role = new TRole();
	role->setId(element.firstChildElement("id").text().toInt());
	role->setName(element.firstChildElement("name").text());
	role->setUserManagement(TConverter::stringToBool(element.firstChildElement("userManagement").text()));
	role->setSystemBackup(TConverter::stringToBool(element.firstChildElement("systemBackup").text()));
	role->setSystemRestore(TConverter::stringToBool(element.firstChildElement("systemRestore").text()));
	role->setDocumentManagement(TConverter::stringToBool(element.firstChildElement("documentManagement").text()));
	role->setDocumentShare(TConverter::stringToBool(element.firstChildElement("documentShare").text()));
	TRolePointer pointer(role);
	return pointer;
}

TDocumentPointer TXMLSerializer::unserializeDocument(QDomElement element)
{
	TDocument *document = new TDocument();
	document->setId(element.firstChildElement("id").text().toInt());
	document->setTitle(element.firstChildElement("title").text());
	document->setFilename(element.firstChildElement("filename").text());
	document->setMimeType(element.firstChildElement("mimeType").text());
	document->setShared(TConverter::stringToBool(element.firstChildElement("shared").text()));
	document->setOwner(unserializeUser(element.firstChildElement("owner")));
	document->setCreateTime(TConverter::stringToDatetime(element.firstChildElement("createTime").text()));
	if(!element.firstChildElement("sharedTime").isNull())
	{
		document->setSharedTime(TConverter::stringToDatetime(element.firstChildElement("sharedTime").text()));
	}
	document->setFavorited(TConverter::stringToBool(element.firstChildElement("favorited").text()));
	if(!element.firstChildElement("latestUsedTime").isNull())
	{
		document->setLatestUsedTime(TConverter::stringToDatetime(element.firstChildElement("latestUsedTime").text()));
	}
	TDocumentPointer pointer(document);
	return pointer;
}
